package com.hex.ds.hdrs.period.job.service;

import com.hex.bigdata.hbda.base.Page;
import com.hex.ds.hdrs.agent.service.AgentHeartbeatLogHisService;
import com.hex.ds.hdrs.agent.service.AgentHeartbeatLogService;
import com.hex.ds.hdrs.common.constant.Const;
import com.hex.ds.hdrs.common.utils.BaseUtil;
import com.hex.ds.hdrs.common.utils.HdrsDateUtil;
import com.hex.ds.hdrs.common.utils.MapUtil;
import com.hex.ds.hdrs.cron.service.CronTaskHisService;
import com.hex.ds.hdrs.cron.service.CronTaskService;
import com.hex.ds.hdrs.log.service.*;
import com.hex.ds.hdrs.period.job.dao.HisBitchDao;
import com.hex.ds.hdrs.period.job.po.HisBitch;
import com.hex.ds.hdrs.period.task.service.PeriodTaskHisService;
import com.hex.ds.hdrs.period.task.service.PeriodTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HisBitchService {

    @Autowired
    private HisBitchDao hisBitchDao;

    @Autowired
    private PeriodJobHisService periodJobHisService;

    @Autowired
    private PeriodTaskHisService periodTaskHisService;

    @Autowired
    private PeriodTaskService periodTaskService;

    @Autowired
    private PeriodJobService periodJobService;

    @Autowired
    private CronTaskService cronTaskService;

    @Autowired
    private CronTaskHisService cronTaskHisService;

    @Autowired
    private AgentHeartbeatLogService agentHeartbeatLogService;

    @Autowired
    private AgentHeartbeatLogHisService agentHeartbeatLogHisService;

    @Autowired
    private RunningStateLogHisService runningStateLogHisService;

    @Autowired
    private RunningStateLogService runningStateLogService;

    @Autowired
    private ServerInteractLogHisService serverInteractLogHisService;

    @Autowired
    private ServerInteractLogService serverInteractLogService;

    @Autowired
    private MailSendLogHisService mailSendLogHisService;

    @Autowired
    private MailSendLogService mailSendLogService;


    public List<HisBitch> selectPages(HisBitch hisBitch, Page page) {
        Map map = MapUtil.convertObjToMap(hisBitch);
        return this.hisBitchDao.selectPages(map, page);
    }

    /**
     * @Method: reduction <br>
     * @Param: [hisBitch] <br>
     * @Return: void <br>
     * @Description：还原备份的所有数据<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/6/3 16:05 <br>
     * @Version： V1.0<br>
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void reduction(HisBitch hisBitch) {
        String type = hisBitch.getBitchType();
        String bitchId = hisBitch.getBitchId();
        if (Const.CYCLE.equals(type)) {
            //周期
            //还原作业实例
            reductionJob(bitchId);
            //还原任务实例
            reductionTask(bitchId);
            //删除历史表数据
            periodJobHisService.delByBitchId(bitchId);
            periodTaskHisService.delByBitchId(bitchId);
        } else if (Const.CRON.equals(type)) {
            //定时任务
            reductionCronTask(bitchId);
            //删除历史表数据
            cronTaskHisService.delByBitchId(bitchId);
        } else if (Const.HEARTBEAT_LOG.equals(type)) {
            //agent心跳日志
            //还原agent心跳日志表
            reductionHeartbeatLogTask(bitchId);
            //删除agent心跳日志历史表
            agentHeartbeatLogHisService.delByBitchId(bitchId);
        } else if (type.equals(Const.RUNNING_LOG)) {
            //运行状况日志
            //还原运行状况日志表
            reductionRunningStateLog(bitchId);
            //删除运行状况日志历史表
            runningStateLogHisService.delByBitchId(bitchId);
        } else if (type.equals(Const.SERVER_LOG)) {
            //服务器交互日志
            //还原服务器交互日志表
            reductionServerLog(bitchId);
            //删除服务器交互日志历史表
            serverInteractLogHisService.delByBitchId(bitchId);
        }else if (type.equals(Const.MAIL_SEND_LOG)) {
            //邮件发送日志
            //还原邮件发送日志表
            reductionMailSendLog(bitchId);
            //删除邮件发送日志历史表
            mailSendLogHisService.delByBitchId(bitchId);
        }

        //修改历史批次表状态
        Map map = new HashMap(16);
        map.put("bitchId", bitchId);
        map.put("isReduction", "YES");
        map.put("reductionTs", HdrsDateUtil.getCurrentTime());
        map.put("reductionUser", BaseUtil.getUserId());
        map.put("upTs", HdrsDateUtil.getCurrentTime());
        map.put("upUser", BaseUtil.getUserId());
        hisBitchDao.updateReductionType(map);
    }

    /**
     * @Method: reductionJob <br>
     * @Param: [bitchId] <br>
     * @Return: void <br>
     * @Description：还原周期作业实例<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/6/3 16:11 <br>
     * @Version： V1.0<br>
     */
    public void reductionJob(String bitchId) {
        periodJobService.reductionJob(bitchId);
    }

    /**
     * @Method: reductionTask <br>
     * @Param: [bitchId] <br>
     * @Return: void <br>
     * @Description：还原周期任务实例<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/6/3 16:12 <br>
     * @Version： V1.0<br>
     */
    public void reductionTask(String bitchId) {
        periodTaskService.reductionTask(bitchId);
    }

    /**
     * @Method: reductionCronTask <br>
     * @Param: [bitchId] <br>
     * @Return: void <br>
     * @Description：还原定时任务实例<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/6/3 16:12 <br>
     * @Version： V1.0<br>
     */
    public void reductionCronTask(String bitchId) {
        cronTaskService.reductionCronTask(bitchId);
    }

    /**
     * @Method: reductionHeartbeatLogTask <br>
     * @Param: [bitchId] <br>
     * @Return: void <br>
     * @Description：还原agent心跳日志数据<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/6/3 16:13 <br>
     * @Version： V1.0<br>
     */
    public void reductionHeartbeatLogTask(String bitchId) {
        agentHeartbeatLogService.reductionHeartbeatLogTask(bitchId);
    }

    /**
     * @Method: reductionRunningStateLog <br>
     * @Param: [bitchId] <br>
     * @Return: void <br>
     * @Description：还原运行状况日志数据<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/6/3 16:13 <br>
     * @Version： V1.0<br>
     */
    public void reductionRunningStateLog(String bitchId) {
        runningStateLogService.reductionRunningStateLog(bitchId);
    }

    /**
     * @Method: reductionServerLog <br>
     * @Param: [bitchId] <br>
     * @Return: void <br>
     * @Description：还原服务器交互日志数据<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/6/10 15:18 <br>
     * @Version： V1.0<br>
     */
    public void reductionServerLog(String bitchId) {
        serverInteractLogService.reductionServerLog(bitchId);
    }

    /**
     * @Method: reductionMailSendLog <br>
     * @Param: [bitchId] <br>
     * @Return: void <br>
     * @Description：还原邮件发送日志数据<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/7/15 14:41 <br>
     * @Version： V1.0<br>
     */
    public void reductionMailSendLog(String bitchId) {
        mailSendLogService.reductionMailSendLog(bitchId);
    }

    /**
     * @Method: qryDayCount <br>
     * @Param: [map] <br>
     * @Return: int <br>
     * @Description：查询数量<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/6/3 16:04 <br>
     * @Version： V1.0<br>
     */
    public int qryDayCount(Map map) {
        return this.hisBitchDao.qryDayCount(map);
    }

    /**
     * @Method: insert <br>
     * @Param: [hisBitch] <br>
     * @Return: void <br>
     * @Description：新增<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/6/3 16:17 <br>
     * @Version： V1.0<br>
     */
    public void insert(HisBitch hisBitch) {
        this.hisBitchDao.insert(hisBitch);
    }
}
